using System;
using System.IO;
using System.Text;

namespace parser.type
{
    public class EbcTypeParser
    {
        public string Parse(string text) {
            var byteArray = Encoding.Default.GetBytes(text);
            var m = new MemoryStream(byteArray);

            var scanner = new Scanner(m);
            var parser = new Parser(scanner);

            using (var stringWriter = new StringWriter()) {
                parser.errors.errorStream = stringWriter;
                parser.Parse();
                if (parser.errors.count != 0) {
                    var message = stringWriter.GetStringBuilder().ToString();
                    throw new Exception(string.Format("Syntax error in 'type' attribute '{0}':\r\n{1}", text, message));
                }
                return parser._resultType;
            }
        }
    }
}